package com.mc.main;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Paint.Cap;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

/**
 * 
 * @author Mikael Bertlin
 *
 */

/**
 * TODO: make a bg view and a front view that holds the finger.
 */

public class GraphDummyView extends View{

	private Paint graphGridPaint;
	private RectF myRect;
	private Paint graphTopBgPaint;
	private Paint graphLinePaint;
	private Paint fingerLinePaint;
	private float cx = 0;
	private float cy = 0;
	private int width = 310;
	private int height = 310;
	private boolean drawFingerLine = false;
	
	
	//0-300
	int[] temp = {100,150,250,200,55,45,65,45,200,54};
	
    public GraphDummyView(Context context, AttributeSet attrs) {
    	super(context, attrs);
        setFocusable(true);
    	setFocusableInTouchMode(true);

        Resources res = this.getContext().getResources();
 
        int graphTopBg = res.getColor(R.color.graphTopBg);
        int graphShadow = res.getColor(R.color.graphShadow);
        int fingerLine = res.getColor(R.color.fingerLine);
        
        graphTopBgPaint = new Paint();
        graphTopBgPaint.setColor(graphTopBg);
        
        int graphGrid = res.getColor(R.color.graphGrid);
        graphGridPaint = new Paint();
        graphGridPaint.setColor(graphGrid); 
       
		myRect = new RectF(10,10,width,height);
		graphTopBgPaint.setShadowLayer(10, 5, 5, graphShadow);
		graphTopBgPaint.setAntiAlias(true);
		
		int graphLine = res.getColor(R.color.graphLine);
		graphLinePaint = new Paint();
		graphLinePaint.setAntiAlias(true);
		graphLinePaint.setColor(graphLine);
		graphLinePaint.setStrokeCap(Cap.ROUND);
		graphLinePaint.setStrokeWidth(5);

		
		fingerLinePaint = new Paint();
		fingerLinePaint.setAntiAlias(true);
		fingerLinePaint.setColor(fingerLine);
		fingerLinePaint.setTextSize(20);
		fingerLinePaint.setStrokeWidth(3);
		
}

    protected void onDraw(Canvas canvas) {
    	//Draw backgroung
    	canvas.drawRoundRect(myRect, 12, 12, graphTopBgPaint);
    	canvas.drawLine(10,60,310,60, graphGridPaint);
    	canvas.drawLine(10,110,310,110, graphGridPaint);
    	canvas.drawLine(10,160,310,160, graphGridPaint);
    	canvas.drawLine(10,210,310,210, graphGridPaint);
    	canvas.drawLine(10,260,310,260, graphGridPaint);
    	
    	//Draw points in temp
    	int oldX = 20;
    	int oldY = temp[0];
    	for(int i=1; i<=temp.length; i++){
    		canvas.drawLine(oldX,oldY,i*30,temp[i-1], graphLinePaint);
    		oldX = i*30;
    		oldY = temp[i-1];
    		
    	}
    	if(drawFingerLine){
    		canvas.drawLine(cx, 10, cx, 310,fingerLinePaint);
    		

    	}
    	canvas.drawCircle(cx, cy, 10, fingerLinePaint);
		canvas.drawText("Time: " + cx, 10, 350, fingerLinePaint);
		canvas.drawText("Temp: " + cy, 10, 370, fingerLinePaint);
    	
    	
    }
    
    private void newTemp(int t){
    	
    	//Add to temp ... rotate
    	
    }
	
	public boolean onTouchEvent(MotionEvent event) {
		this.invalidate();
		
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			 Log.v("T", "DOWN");
			 graphLinePaint.setStrokeWidth(10);
			 drawFingerLine = true;
			 return true;
		} else if(event.getAction() == MotionEvent.ACTION_UP){
			 Log.v("T", "UP");
			 graphLinePaint.setStrokeWidth(5);
			 drawFingerLine = false;
			 return true;
		} else if(event.getAction() == MotionEvent.ACTION_MOVE){
			int posY = (int) (event.getX() / 30);
			if(posY >= 9)
				posY = 9;
			if(posY <=0)
				posY = 0;
			cy =  temp[posY];
			cx = event.getX();
			if((int)cx >= (width - 20))
				cx = width;
			if((int)cx <= 20)
				cx = 20;
			Log.v("MOVE", "x: " + cx + "y: " + cy);
			
			return true;
			
		}
		
		return false;
		
	
	}
    
}
